package com.cskaoyan.market.service.admin.impl;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.mapper.MarketAdminMapper;
import com.cskaoyan.market.service.admin.MarketAdminService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MarketAdminServiceImpl implements MarketAdminService {
    @Autowired
    MarketAdminMapper adminMapper;

    @Override
    public List<MarketAdmin> list(Integer page, Integer limit, String username, String sort, String order) {
        // sort若为空，则取默认值add_time
        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        // order若为空，则取默认值desc - 降序
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        //当我们执行当前对象的方法时，那么执行的便是对应的sql语句
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.or().andDeletedEqualTo(false);
        if (username != null) {
            criteria.andUsernameLike("%"+ username + "%");
        }
        PageHelper.startPage(page,limit);
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(marketAdminExample);

        return marketAdmins;
    }

    @Override
    public List<MarketAdmin> create(String password, String avatar, Integer[] roleIds, String username) {

        //构建一个MarketAdminExample
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        //new一个MarketAdmin对象
        MarketAdmin marketAdmin = new MarketAdmin();
        //给对象里写入数据
        marketAdmin.setAddTime(LocalDateTime.now());
        marketAdmin.setAvatar(avatar);
        marketAdmin.setPassword(password);
        marketAdmin.setRoleIds(roleIds);
        marketAdmin.setUpdateTime(LocalDateTime.now());
        marketAdmin.setUsername(username);
        //给admin里新增一个成员
        adminMapper.insertSelective(marketAdmin);
        marketAdminExample.or().andLogicalDeleted(true);
        List<MarketAdmin> adminCreat = adminMapper.selectByExample(marketAdminExample);


        return adminCreat;
    }

    @Override
    public int delete(Integer id) {


        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setDeleted(true);

        MarketAdminExample adminExample = new MarketAdminExample();
        adminExample.or().andIdEqualTo(id);
        int row = adminMapper.updateByExampleSelective(marketAdmin, adminExample);


        return row;
    }

    @Override
    public int update(Integer id, String username, String avatar, Integer[] roleIds, String password) {
        //session域

        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setUsername(username);
        marketAdmin.setAvatar(avatar);
        marketAdmin.setPassword(password);
        marketAdmin.setRoleIds(roleIds);
        marketAdmin.setUpdateTime(LocalDateTime.now());

        MarketAdminExample adminExample = new MarketAdminExample();
        adminExample.or().andIdEqualTo(id);
        int row = adminMapper.updateByExampleSelective(marketAdmin, adminExample);

        return row;
    }

    @Override
    public List<MarketAdmin> listByName(String username) {

        //当我们执行当前对象的方法时，那么执行的便是对应的sql语句
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.or().andLogicalDeleted(true);
        marketAdminExample.orderBy("id desc");
        PageHelper.startPage(1,20);
        List<MarketAdmin> marketAdmins = adminMapper.selectByExampleSelective(marketAdminExample, MarketAdmin.Column.id, MarketAdmin.Column.username, MarketAdmin.Column.avatar, MarketAdmin.Column.roleIds);

        return marketAdmins;
    }


}
